#include "input_dir.h"


extern pInput_dev head_input_dev;


Input_dir current_input_dir = {
	.find_dev = current_find_dev,
	.bind_dev = current_bind_dev,
	.init_dev = current_init_dev,
	.exit_dev = current_exit_dev,
	.get_current_data =  get_current_input_data,
	.pthread_fun = current_pthread_fun,
};


void current_find_dev(pInput_dir current_dir,char * name) {
	pInput_dev buf_dev = head_input_dev->next;
	if(head_input_dev == NULL) {
		printf("this err\n");
	}
	while(buf_dev != NULL && strcmp(name,buf_dev->name) !=0) {
		buf_dev = buf_dev->next;
	}
	if(buf_dev != NULL) { 
		current_dir->pcurrent_dev = buf_dev;
		current_dir->dev_name = buf_dev->name;
		current_dir->bind_dev(current_dir,current_dir->name);
	}
}

int current_bind_dev(pInput_dir current_dir,char* name) {
	if(current_dir->pcurrent_dev->bind_dir(name) != 0) {  //dev 绑定  div
		printf("the bind_current_dev err \n");
		return -1;  
	}
	return 0;
}

int current_init_dev(pInput_dir current_dir) {
	pthread_t pid;
	if(current_dir->pcurrent_dev->init_dev() < 0) {
		printf("div init dev err \n");
		return -1;
	}
	//开一个线程一直接受
	pthread_create(&pid,NULL,current_pthread_fun,(void *)current_dir);
	return 0;
}

int current_exit_dev(pInput_dir current_dir) {
	if(current_dir->pcurrent_dev->exit_dev() < 0) {
		printf("div init dev err \n");
		return -1;
	}
	return 0;
}


pInput_data get_current_input_data(pInput_dir current_dir) {
	return current_dir->pcurrent_dev->get_input_data();
}

void * current_pthread_fun(void * arg) {
	pInput_dir current_dir = (pInput_dir)arg;
	printf("this pthread create \n");
	while(1) {
		pInput_data current_data= current_dir->get_current_data(current_dir);
		//拿到数据，写进去，并唤醒  
		push_data_to_range(current_data);
	}
}




